package com.lbs.jiepiaisi.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.base.BaseActivity;
import com.lbs.jiepiaisi.bean.BaseResponseEntity;
import com.lbs.jiepiaisi.biz.UpdatePwdBiz;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.ToastUtil;
import com.lbs.jiepiaisi.view.WaitingDialogControll;

/**
 *
 * 时间：2017/8/18 11:18
 * 作用：用户登录修改密码
 */

public class AccountChangePassWordActivity extends BaseActivity implements View.OnClickListener{

    private Button mSubmitBtn;

    private EditText mOldPwdEt;

    private EditText mNewPwdEt;

    private EditText mReNewPwdEt;

    private UpdatePwdReceiver updatePwdReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isTemplate=true;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_changepassword);
        titleView.setText("修改密码");
        templateButtonLeft.setImageResource(R.mipmap.back_white);
        updatePwdReceiver = new UpdatePwdReceiver();
        getApplicationContext().registerReceiver(updatePwdReceiver, new IntentFilter(Constant.ACCOUNT_UPDATE_PWD));
        findView();
        setListener();
    }

    private void findView() {
        mSubmitBtn = (Button)findViewById(R.id.btn_submit);

        mOldPwdEt = (EditText) findViewById(R.id.et_old_pwd);

        mNewPwdEt = (EditText) findViewById(R.id.et_new_pwd);

        mReNewPwdEt = (EditText)findViewById(R.id.et_renew_pwd);
    }

    private void setListener(){
        mSubmitBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_submit:
                if("".equals(mOldPwdEt.getText().toString())){
                    ToastUtil.show("请输入旧密码");
                    break;
                }
                if("".equals(mNewPwdEt.getText().toString())){
                    ToastUtil.show("请输入新密码");
                    break;
                }
                if("".equals(mReNewPwdEt.getText().toString())){
                    ToastUtil.show("请输入重复新密码");
                    break;
                }
                if(!mNewPwdEt.getText().toString().equals(mReNewPwdEt.getText().toString())){
                    ToastUtil.show("新密码与确认密码不一致");
                    break;
                }
                WaitingDialogControll.showWaitingDialog(this);
                new UpdatePwdBiz().updatePwdBiz(mNewPwdEt.getText().toString());
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getApplicationContext().unregisterReceiver(updatePwdReceiver);
    }

    class UpdatePwdReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            WaitingDialogControll.dismissWaitingDialog();

            BaseResponseEntity baseResponseEntity =(BaseResponseEntity) intent.
                    getSerializableExtra(Constant.KEY_DATA);

            if(baseResponseEntity.getStatus()==Constant.STATUS_OK){
                ToastUtil.show("修改成功");
                finish();
            }
            else if(baseResponseEntity.getStatus()==Constant.STATUS_NETWORK_ERROR){
                ToastUtil.show("修改失败");
            }else if(baseResponseEntity.getStatus()==Constant.STATUS_NO_NET){
                ToastUtil.show("请检查网络");
            }
        }
    }
}
